var gulp = require("gulp");
var jade = require("gulp-jade");
var less = require('gulp-less');
var plumber = require("gulp-plumber");
var webserver = require('gulp-webserver');

var jadeFiles = {src:'./views/*.jade',dist:'./public/'};
gulp.task('jade',function(){
    gulp.src(jadeFiles.src)
        .pipe(jade({pretty:true}))
        .pipe(plumber())
        .pipe(gulp.dest(jadeFiles.dist))
});

var jsFiles = {src:"./src/js/*.js",dist:"./public/js"};

gulp.task('js', function() {
    gulp.src(jsFiles.src)
        .pipe(gulp.dest(jsFiles.dist))
});
var lessFile = {src:'./src/less/*.less',dist:'./public/css/'};

gulp.task('less',function () {
    gulp.src(lessFile.src)
        .pipe(less().on("error",function(e){
            console.error(e.message);
            this.emit('end');
        }))
        .pipe(gulp.dest(lessFile.dist));

});

gulp.task('assets',function(){
    gulp.src('./src/assets/*.?(png|jpg|gif|svg)')
        .pipe(gulp.dest("./public/assets/"))

});

gulp.task('watch',['webserver'], function () {
    gulp.watch('./src/*.jade', ['jade']);
    gulp.watch('./src/less/*.less', ['less']);
    gulp.watch('./src/img/*.?(png|jpg|gif|svg)', ['assets']);
    gulp.watch('./src/js/*.js', ['js']);
});



gulp.task('webserver', function() {
    gulp.src('./dist')
        .pipe(webserver({
            livereload: true,
            port:4000,
            host:"127.0.0.1",
            fallback: 'index.html'
        }));
});


gulp.task('default',['watch','jade','js','less','assets','webserver'],function () {

});
